package com.starlink.common.annotation.client;//package com.zhailijia.client;
//
//import com.zhailijia.client.model.weixin.WeixinNativeCreateOrderReq;
//import com.zhailijia.client.model.weixin.WeixinNativeCreateOrderRes;
//import com.zhailijia.client.model.weixin.WeixinQueryOrderRes;
//import com.zhailijia.common.enums.ErrorCode;
//import com.zhailijia.common.exception.BusinessException;
//import com.zhailijia.utils.FastJsonUtils;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.http.client.methods.CloseableHttpResponse;
//import org.apache.http.client.methods.HttpGet;
//import org.apache.http.client.methods.HttpPost;
//import org.apache.http.entity.StringEntity;
//import org.apache.http.impl.client.CloseableHttpClient;
//import org.apache.http.util.EntityUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.nio.charset.Charset;
//
///**
// * native:  https://pay.weixin.qq.com/wiki/doc/apiv3/open/pay/chapter2_7_0.shtml
// */
//@Slf4j
//@Component
//public class WeixinNativeClient {
//
//  @Autowired
//  private CloseableHttpClient httpClient;
//
//  /**
//   *
//   * 创建支付单: https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_4_1.shtml
//   * 微信native支付生成的二维码有有效期吗?
//   * 一个二维码的有效期是根据Native下单接口返回的code_url决定，code_url的有效期是2小时。
//   * @throws Exception
//   */
//  public WeixinNativeCreateOrderRes createOrder(WeixinNativeCreateOrderReq req) throws IOException {
//    CloseableHttpResponse response = null;
//    try {
//      HttpPost httpPost = new HttpPost("https://api.mch.weixin.qq.com/v3/pay/transactions/native");
//      StringEntity entity = new StringEntity(FastJsonUtils.toJSONString(req), Charset.forName("UTF-8"));
//      entity.setContentType("application/json");
//      httpPost.setEntity(entity);
//      httpPost.setHeader("Accept", "application/json");
//
//      //完成签名并执行请求
//      response = httpClient.execute(httpPost);
//
//      int statusCode = response.getStatusLine().getStatusCode();
//      if (statusCode == 200) { //处理成功
//        log.info("WeixinNativeClient.createOrder success,return body: {}", EntityUtils.toString(response.getEntity()));
//      } else if (statusCode == 204) { //处理成功，无返回Body
//        log.info("WeixinNativeClient.createOrder success");
//      } else {
//        log.error("WeixinNativeClient.createOrder failed,resp code: {}, ,return body = {}", statusCode, EntityUtils.toString(response.getEntity()));
//        throw new IOException("request failed");
//      }
//      return FastJsonUtils.fromJSONString(EntityUtils.toString(response.getEntity()), WeixinNativeCreateOrderRes.class);
//    }catch (Exception e){
//      log.error("WeixinNativeClient.createOrder err: {}", e.getMessage(), e);
//      throw new BusinessException(ErrorCode.ERR_REMOTE_INVOKE, e);
//    } finally {
//      if(response != null) {
//        response.close();
//      }
//    }
//  }
//
//  /**
//   * 查询支付单
//   * https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/1217752501201407033233368018?mchid=1230000109
//   * @throws Exception
//   */
//  public WeixinQueryOrderRes queryOrder(String outTradeNo, String mchid) throws IOException {
//    CloseableHttpResponse response = null;
//    try {
//      HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/" + outTradeNo + "?mchid=" + mchid);
//      httpGet.setHeader("Accept", "application/json");
//      //完成签名并执行请求
//      response = httpClient.execute(httpGet);
//      int statusCode = response.getStatusLine().getStatusCode();
//      if (statusCode == 200) { //处理成功
//        log.info("WeixinNativeClient.queryOrder success,return body: {}", EntityUtils.toString(response.getEntity()));
//      } else if (statusCode == 204) { //处理成功，无返回Body
//        log.info("WeixinNativeClient.queryOrder success");
//      } else {
//        log.error("WeixinNativeClient.queryOrder failed,resp code: {}, ,return body = {}", statusCode, EntityUtils.toString(response.getEntity()));
//        throw new IOException("request failed");
//      }
//      return FastJsonUtils.fromJSONString(EntityUtils.toString(response.getEntity()), WeixinQueryOrderRes.class);
//    }catch (Exception e){
//      log.error("WeixinNativeClient.queryOrder err: {}", e.getMessage(), e);
//      throw new BusinessException(ErrorCode.ERR_REMOTE_INVOKE, e);
//    } finally {
//      if(response != null) {
//        response.close();
//      }
//    }
//  }
//
//}
